<?php
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Document\Controller;
use \Common\Controller\BaseController;
define('MODULE_CACHE', DATA_PATH.'caches_model/');
Class AnswerController extends BaseController {
    public function _initialize() {
    	parent::_initialize();
        $this->categorys = getcache('category', 'commons');
        $this->modelid = 8;
        $this->models = getcache('model', 'commons');
        $this->userid = (int) cookie('_userid');
        /* 将页码变量赋值到模板 */
    }

    /**
     * 主页
     * @author xuewl <master@xuewl.com>
     */
    public function answer() {
        $param = I('param.');
        extract($param);
        $aid = (int) $aid;
        $model = $this->models[$this->modelid];
        if($this->userid < 1) $this->error('您尚未登陆，请登陆后答题');
        if($aid < 1 || empty($model)) $this->error('参数提交错误');
        $system_info = M($model['tablename'])->getById($aid);
        $model_info = M($model['tablename'].'_data')->getById($aid);

        /* 判断重复回答 */
        $log_map = array();
        $log_map['uid'] = $this->userid;
        $log_map['id_encode'] = id_encode($this->modelid, $aid);
        if($system_info['repeat_reward'] == 0 && M($model['tablename'].'_log')->where($log_map)->count()) {
            $this->error('您已回答本问题，请勿重复回答');
        }
        $answer_config = json_decode($model_info['answer_config'], TRUE);
        $answer = explode(",", $answer);
        $diff_arr = array_diff($answer, $answer_config[$answer_config['type']]);
        if (empty($diff_arr)) {
            $log_map['dateline'] = NOW_TIME;
            $result = M($model['tablename'].'_log')->add($log_map);
            if(!$result) {
                $this->error('系统异常，请稍候重试');
            }
            M('Member')->where(array('userid' => $this->userid))->setInc("u_coin", $system_info['single_reward']);
            $this->success('恭喜成功回答该组问题并获得'.$system_info['single_reward'].'个U币');
        } else {
            $this->error('您选择的答案不正确');
        }
    }
}